﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class UEnter : MonoBehaviour {

	// 适应手机端改造
	private RoundUi roundUi;
	private Transform elementBottonDowm = null;

	private void Start()
    {
		// 适应手机端改造
		roundUi = this.transform.GetComponent<RoundUi>();
		// 绑定按键事件
		var elementTra = this.transform.Find("Element");
		for (int i = 0; i < elementTra.childCount; i++)
		{
			Transform tmp = elementTra.GetChild(i).transform;
			tmp.GetComponent<Button>()?.onClick.AddListener(
					() => { elementBottonDowm = tmp; }
				);
		}
	}

    private void LateUpdate(){
		/*
		GameObject tempClick = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject;
		if(tempClick!=null&&tempClick.transform.IsChildOf(this.transform)){
			if(Input.GetMouseButtonDown(0))
			{
				RoundUi tempRoundUiC=tempClick.GetComponentInParent<RoundUi>();
				if(tempRoundUiC!=null){
					if(tempRoundUiC.CheckFrontTra(tempClick.transform)){
						tempRoundUiC.SetAutoStopTime();
						Debug.Log("click  " + tempRoundUiC.GetFrontTra().name);
						SudokuCreate.SelectSudoku(tempRoundUiC.GetFrontTra().name);
						SudokuCreate.printSudoku();
					}
				}
			}
		}
		*/
		// 适应手机端改造
		if (elementBottonDowm != null)
		{
            if (roundUi != null)
            {
				if (roundUi.CheckFrontTra(elementBottonDowm))
				{
					roundUi.SetAutoStopTime();
#if UNITY_EDITOR
					Debug.Log("click  " + roundUi.GetFrontTra().name);
#endif
					SudokuCreate.SelectSudoku(roundUi.GetFrontTra().name);
					SudokuCreate.printSudoku();
					elementBottonDowm = null;
					SceneManager.LoadScene(2);
				}
                else
                {
					elementBottonDowm = null;
				}
			}
		}
	}
}
